#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\utility;


init()
{
    precacheModel("projectile_semtex_grenade_bombsquad");
    precacheModel("weapon_c4_bombsquad");
    
    for(;;) {
        level waittill("something");
        level.zHelicopter = littlebird_Spawn((-10460, -495, 5530), (0, 45, 0));
        
        level.zHelicopter waittill("death");
    }
    
}

littlebird_Spawn(spawn_origin, spawn_angles)
{
    chopper = spawnHelicopter(level.zMasterBot, spawn_origin, spawn_angles, "littlebird_mp" , "vehicle_little_bird_armed");
    //chopper.idle = spawn("script_model", spawn_origin);
    //chopper.idle.angles = spawn_angles;
    //chopper.idle setModel("vehicle_little_bird_armed");
    chopper.damageCallback = ::littlebird_VehicleDamage; // callback
    chopper.health = 1000;
    chopper.maxhealth = 1000;
    chopper.owner = level.zMasterBot;
    chopper.team = level.zCfg_Attackers;
    chopper setCanDamage(true);
    chopper vehicle_SetSpeed(250, 135);
    chopper thread littlebird_Existence();
    chopper thread littlebird_Destroy();
    
    
    chopper.seats = [];
    chopper.seats["pilot"] = spawn("script_origin", chopper.origin); // место пилота
    chopper.seats["pilot"].busy = undefined; // игрок
    chopper.seats["pilot"] enableLinkTo();
    chopper.seats["pilot"] linkTo(chopper, "tag_origin", (40, 15, -85), (0, 0, 0));
    
    chopper.seats["gunlayer"] = spawn("script_origin", chopper.origin); // место наводчика
    chopper.seats["gunlayer"].busy = undefined;
    chopper.seats["gunlayer"] enableLinkTo();
    chopper.seats["gunlayer"] linkTo(chopper, "tag_origin", (40, -15, -85), (0, 0, 0));
    
    
    chopper.vectors = [];
    chopper.vectors["pilotguide"] = spawn("script_origin", chopper.origin);
    //chopper.vectors["pilotguide"] setModel("tag_origin");
    
    
    chopper.vectors["lrocketfrom"] = spawn("script_model", chopper.origin);
    chopper.vectors["lrocketfrom"] setModel("projectile_semtex_grenade_bombsquad");
    chopper.vectors["lrocketfrom"] linkTo(chopper, "tag_origin", (-50, 50, -80), (0, 0, 0));
    chopper.vectors["lrocketto"] = spawn("script_model", chopper.origin);
    chopper.vectors["lrocketto"] setModel("projectile_semtex_grenade_bombsquad");
    chopper.vectors["lrocketto"] linkTo(chopper, "tag_origin", (50, 50, -80), (0, 0, 0));
    
    chopper.vectors["rrocketfrom"] = spawn("script_model", chopper.origin);
    chopper.vectors["rrocketfrom"] setModel("projectile_semtex_grenade_bombsquad");
    chopper.vectors["rrocketfrom"] linkTo(chopper, "tag_origin", (-50, -50, -80), (0, 0, 0));
    chopper.vectors["rrocketto"] = spawn("script_model", chopper.origin);
    chopper.vectors["rrocketto"] setModel("projectile_semtex_grenade_bombsquad");
    chopper.vectors["rrocketto"] linkTo(chopper, "tag_origin", (50, -50, -80), (0, 0, 0));
    
    
    //wait(0.5);
    //chopper hide();
    
    
    chopper thread littlebird_CollideWorld();
    chopper thread littlebird_PlayersWaiter();
    
    
    return chopper;
}

littlebird_Buttons(player)
{
    player endon("death");
    player endon("disconnect");
    self endon("death");
    self endon("helicopter_gone");
    
    
    /// THESE BUTTONS BIND NOTIFIES ARE HERE TEMPORARILY ///
    player notifyOnPlayerCommand("btn_W_down", "+forward");
    player notifyOnPlayerCommand("btn_W_up", "-forward");
    
    player notifyOnPlayerCommand("btn_S_down", "+back");
    player notifyOnPlayerCommand("btn_S_up", "-back");
    
    player notifyOnPlayerCommand("btn_A_down", "+moveleft");
    player notifyOnPlayerCommand("btn_A_up", "-moveleft");
    
    player notifyOnPlayerCommand("btn_D_down", "+moveright");
    player notifyOnPlayerCommand("btn_D_up", "-moveright");
    
    player notifyOnPlayerCommand("btn_SPACE_down", "+gostand");
    player notifyOnPlayerCommand("btn_SPACE_up", "-gostand");
    
    player notifyOnPlayerCommand("btn_SHIFT_down", "+breath_sprint");
    player notifyOnPlayerCommand("btn_SHIFT_up", "-breath_sprint");
    
    player notifyOnPlayerCommand("btn_CTRL", "toggleprone");
    
    
    self.zControl_Offset = (0, 0, 0);
    
    
    for(;;) {
        action = player z_Waittill13AnyReturn("btn_W_down", "btn_W_up", "btn_S_down", "btn_S_up",
        "btn_A_down", "btn_A_up", "btn_D_down", "btn_D_up",
        "btn_SPACE_down", "btn_SPACE_up", "btn_SHIFT_down", "btn_SHIFT_up", "btn_CTRL");
        
        switch(action) {
        case "btn_W_down":
            self.zControl_Offset = self.zControl_Offset + (500, 0, 0);
            break;
        
         case "btn_W_up":
            self.zControl_Offset = self.zControl_Offset + (-500, 0, 0);
            break;
        
         case "btn_S_down":
            self.zControl_Offset = self.zControl_Offset + (-500, 0, 0);
            break;
        
         case "btn_S_up":
            self.zControl_Offset = self.zControl_Offset + (500, 0, 0);
            break;
        
         case "btn_A_down":
            self.zControl_Offset = self.zControl_Offset + (0, 500, 0);
            break;
        
         case "btn_A_up":
            self.zControl_Offset = self.zControl_Offset + (0, -500, 0);
            break;
        
         case "btn_D_down":
            self.zControl_Offset = self.zControl_Offset + (0, -500, 0);
            break;
        
         case "btn_D_up":
            self.zControl_Offset = self.zControl_Offset + (0, 500, 0);
            break;
        
         case "btn_SPACE_down":
            self.zControl_Offset = self.zControl_Offset + (0, 0, 500);
            break;
        
         case "btn_SPACE_up":
            self.zControl_Offset = self.zControl_Offset + (0, 0, -500);
            break;
        
         case "btn_SHIFT_down":
            self.zControl_Offset = self.zControl_Offset + (0, 0, -500);
            break;
        
         case "btn_SHIFT_up":
            self.zControl_Offset = self.zControl_Offset + (0, 0, 500);
            break;
        
        case "btn_CTRL":
            self.zControl_Offset = (0, 0, 0);
            player iPrintLn("^1Resetted");
            break;
        }
    }
}

littlebird_CollideWorld()
{
    self endon("death");
    self endon("helicopter_gone");
    
    for(;;) {
        if(!bulletTracePassed(self.origin, self.origin + (150, 0, 0), false, self)) {
            wait(0.2);
            self vehicle_FinishDamage(self.seats["pilot"].busy, self.seats["pilot"].busy, 9999, 0, "MOD_PROJECTILE_SPLASH", "heli_remote_mp", self.origin, (0, 0, 0), "none", 0, 0, "body_animate_jnt");
        }
        
        wait(0.025);
        
        if(!bulletTracePassed(self.origin, self.origin + (0, 150, 0), false, self)) {
            wait(0.2);
            self vehicle_FinishDamage(self.seats["pilot"].busy, self.seats["pilot"].busy, 9999, 0, "MOD_PROJECTILE_SPLASH", "heli_remote_mp", self.origin, (0, 0, 0), "none", 0, 0, "body_animate_jnt");
        }
        
        wait(0.025);
        
        if(!bulletTracePassed(self.origin, self.origin + (-150, 0, 0), false, self)) {
            wait(0.2);
            self vehicle_FinishDamage(self.seats["pilot"].busy, self.seats["pilot"].busy, 9999, 0, "MOD_PROJECTILE_SPLASH", "heli_remote_mp", self.origin, (0, 0, 0), "none", 0, 0, "body_animate_jnt");
        }
        
        wait(0.025);
        
        if(!bulletTracePassed(self.origin, self.origin + (0, -150, 0), false, self)) {
            wait(0.2);
            self vehicle_FinishDamage(self.seats["pilot"].busy, self.seats["pilot"].busy, 9999, 0, "MOD_PROJECTILE_SPLASH", "heli_remote_mp", self.origin, (0, 0, 0), "none", 0, 0, "body_animate_jnt");
        }
        
        wait(0.025);
    }
}

littlebird_Control(player)
{
    player endon("death");
    player endon("disconnect");
    self endon("death");
    self endon("helicopter_gone");
    
    for(;;) {
        self.vectors["pilotguide"] linkTo(self, "tag_origin", self.zControl_Offset, (0, 0, 0));
        
        self setVehGoalPos(self.vectors["pilotguide"].origin + (0, 0, 10), true);
        
        wait(0.1);
        //self.vectors["pilotguide"] unlink();
    }
}

littlebird_Destroy()
{
    self endon("leaving");
    self endon("helicopter_gone");
    
    self waittill("death");
    
    if(!isDefined(self))
        return;
    
    self vehicle_SetSpeed(25, 5);
    self thread littlebird_Spin(randomIntRange(180, 220));
    
    wait(randomFloatRange(2.5, 5.0));
    
    self littlebird_Explode();
}

littlebird_Existence()
{
    self waittill_any("crashing", "leaving");
    
    self notify("helicopter_gone");
}

littlebird_Explode()
{
    forward = (self.origin + (0, 0, 1)) - self.origin;
    playFx(level.chopper_fx["explode"]["death"]["cobra"], self.origin, forward);
    
    // play heli explosion sound
    self playSound("cobra_helicopter_crash");
    self notify("explode");
    
    if(isDefined(self.seats["pilot"].busy))
        self.seats["pilot"].busy suicide();
    
    if(isDefined(self.seats["gunlayer"].busy))
        self.seats["gunlayer"].busy suicide();
    
    foreach(seat in self.seats)
        seat delete();
    
    foreach(vector in self.vectors)
        vector delete();
    
    //self.idle delete();
    self delete();
}

littlebird_PlayersWaiter()
{
    self endon("death");
    self endon("helicopter_gone");
    
    for(;;) {
        level waittill("zplayer_wants_choppa", player);
        
        if(distance2d(self.origin, player.origin) > 200)
            continue;
        
        if(!isDefined(self.seats["pilot"].busy)) {
            self thread littlebird_SeatPilot(player);
        } else if(!isDefined(self.seats["gunlayer"].busy)) {
            self thread littlebird_SeatGunlayer(player);
        }
    }
}

littlebird_SeatGunlayer(player)
{
    self.seats["gunlayer"].busy = player;
    
    //self.idle hide();
    //self show();
    
    player disableWeapons();
    
    player playerLinkToDelta(self.seats["gunlayer"], undefined, 0, 120, 120, 180, 180, false);
}

littlebird_SeatPilot(player)
{
    self.seats["pilot"].busy = player;
    
    //self.idle hide();
    //self show();
    
    player disableWeapons();
    
    player playerLinkToDelta(self.seats["pilot"], undefined, 0, 120, 120, 180, 180, false);
    //player PlayerLinkedOffsetEnable();
    
    self thread littlebird_Buttons(player);
    self thread littlebird_Control(player);
}

littlebird_Spin(speed)
{
    self endon("explode");
    
    // tail explosion that caused the spinning
    playFxOnTag(level.chopper_fx["explode"]["medium"], self, "tail_rotor_jnt");
    playFxOnTag(level.chopper_fx["fire"]["trail"]["medium"], self, "tail_rotor_jnt");
    
    self setYawSpeed(speed, speed, speed);
    
    while(isDefined(self)) {
        self setTargetYaw(self.angles[1] + (speed*0.9));
        wait(1.0);
    }
}

littlebird_VehicleDamage(inflictor, attacker, damage, dFlags, meansOfDeath, weapon, point, dir, hitLoc, timeOffset, modelIndex, partName)
{
    if(attacker == self)
        return;
    
    //attacker maps\mp\gametypes\_damagefeedback::updateDamageFeedback("");
    
    self vehicle_FinishDamage(inflictor, attacker, damage, dFlags, meansOfDeath, weapon, point, dir, hitLoc, timeOffset, modelIndex, partName);
}

